package com.yisu.expressway.manager.website.controller.orderMoudle;

import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.LogisticsFiles;
import com.yisu.expressway.ewf.entity.OrderMain;
import com.yisu.expressway.ewf.entity.OrderSub;
import com.yisu.expressway.manager.core.service.ILogisticsFilesService;
import com.yisu.expressway.manager.core.service.IOrderService;
import com.yisu.expressway.manager.domain.request.OrderRequest;
import com.yisu.expressway.manager.website.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.FAILURE;

/**
 * /**
 * User: 赵杰
 * Date: 2017/5/8
 * Time: 17:21
 * Desc: 订单管理类 .
 */
@Controller
@RequestMapping(value = "/orderManager")
public class OrderManagerController extends BaseController {
    @Resource
    private IOrderService iOrderService;
    @Resource
    private ILogisticsFilesService iLogisticsFilesService;

    @RequestMapping(value = "/index.htm")
    public String listPage(Model model, OrderRequest orderRequest) {
        try {
            if (!StringUtils.isEmpty(orderRequest.getKeyword())) {
                orderRequest.setKeyword(java.net.URLDecoder.decode(orderRequest.getKeyword(), "utf-8"));
            }
            if (!StringUtils.isEmpty(orderRequest.getOrderStatus()) && orderRequest.getOrderStatus().equals("999")) {
                orderRequest.setOrderStatus(null);
            }
            EasyUIGrid<OrderMain> orderMainVos = iOrderService.getOrderMainListByPage(orderRequest);
            model.addAttribute("orderMainVos", orderMainVos.getRows());
            model.addAttribute("currentPage", orderRequest.getPage());
            model.addAttribute("totalPages", (orderRequest.getTotalRecords() / orderRequest.getRows()) + 1);
            model.addAttribute("keyword", orderRequest.getKeyword());
            model.addAttribute("orderId", orderRequest.getOrderId());
            model.addAttribute("orderStatus", orderRequest.getOrderStatus());
            model.addAttribute("titleName", "订单列表");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "/orderModule/orderManager/index";
    }

    @RequestMapping(value = "/orderDetail.htm")
    public String orderDetail(Model model, OrderRequest orderRequest) {
        try {
            OrderMain orderMain = iOrderService.getOrderMainByOrderId(Long.parseLong(orderRequest.getOrderId()));
            List<OrderSub> orderSubList = iOrderService.getOrderSubByOrderId(Long.parseLong(orderRequest.getOrderId()));
            List<LogisticsFiles> logisticsFiles = iLogisticsFilesService.queryLogisticsFiles();
            model.addAttribute("titleName", "订单详情页面");
            model.addAttribute("orderMain", orderMain);
            model.addAttribute("orderSubVos", orderSubList);
            model.addAttribute("logisticsFilesVos", logisticsFiles);
        } catch (Exception ex) {
            logger.error("订单详情页面加载异常：" + ex.getMessage(), ex);
        }
        return "/orderModule/orderManager/orderDetail";
    }

    /**
     * 编辑订单信息
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/modifyOrderDetail.json")
    @ResponseBody
    public JsonResult modifyOrderDetail(Model model, OrderMain orderMain) {
        try {
            OrderMain orderMainVo = iOrderService.getOrderMainByOrderId(orderMain.getOrderId());
            if (orderMain.getDiscountAmount() != null) {
                orderMain.setOrderAmount(orderMainVo.getGoodsAllAmount().subtract(orderMain.getDiscountAmount()));
            }
            if (iOrderService.modifyOrderMain(orderMain)) {
                return new JsonResult();
            } else {
                return new JsonResult(FAILURE.code, "操作失败！");
            }
        } catch (Exception ex) {
            return new JsonResult(FAILURE.code, "信息不合法！");
        }
    }

    /**
     * 订单发货操作
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/deliverGoodsAction.json")
    @ResponseBody
    public JsonResult deliverGoodsAction(Model model, OrderMain orderMain) {
        try {
            if (StringUtils.isEmpty(orderMain.getLogisticsName()) || StringUtils.isEmpty(orderMain.getLogisticsOrderId())) {
                return new JsonResult(FAILURE.code, "物流单号不能为空！");
            }
            OrderMain updateModel = new OrderMain();
            OrderMain orderMainVo = iOrderService.getOrderMainByOrderId(orderMain.getOrderId());
            updateModel.setOrderId(orderMainVo.getOrderId());
            updateModel.setLogisticsOrderId(orderMain.getLogisticsOrderId());
            String[] logisticsinfo = orderMain.getLogisticsName().split("-");
            updateModel.setLogisticsName(logisticsinfo[1]);
            updateModel.setLogisticsNo(logisticsinfo[0]);
            updateModel.setOrderStatus(OrderMain.OrderStatus.WAIT_RECEIVE_GOODS.code);
            if (iOrderService.modifyOrderMain(updateModel)) {
                return new JsonResult();
            } else {
                return new JsonResult(FAILURE.code, "操作失败！");
            }

        } catch (Exception e) {
            logger.error("订单发货操作异常:" + e.getMessage(), e);
        }
        return new JsonResult();
    }

    /**
     * 取消订单
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/cancelOrder.json")
    @ResponseBody
    public JsonResult cancelOrder(Model model, OrderMain orderMain) {
        try {
            try {
                OrderMain updateModel = new OrderMain();
                if (orderMain.getOrderId() == null || StringUtils.isEmpty(orderMain.getOrderAbstract())) {
                    return new JsonResult(FAILURE.code, "信息不合法！");
                }
                if (iOrderService.cancelOrder(orderMain)) {
                    return new JsonResult();
                } else {
                    return new JsonResult(FAILURE.code, "操作失败！");
                }
            } catch (Exception ex) {
                return new JsonResult(FAILURE.code, "信息不合法！");
            }

        } catch (Exception e) {
            logger.error("取消订单异常:" + e.getMessage(), e);
        }
        return new JsonResult();
    }
}
